package com.newbee.fragement.reg;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.xbill.DNS.EDNSOption.Code;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hhu.newbee.R;
import com.newbee.activity.regist.RegisterActivity;
import com.newbee.app.OnRefrushActivity;
import com.newbee.service.MainService;
import com.newbee.service.MainServiceKey;
import com.newbee.service.MainServiceTask;
import com.newbee.service.TaskType;
import com.newbee.util.StringUtil;

public class RegEmailFragement extends Fragment implements OnClickListener,
		OnRefrushActivity
{

	private TextView email;

	private String code = null;

	private Handler mHandler = new Handler();
	private Button getCodeBtn = null;

	private int time;

	private EditText regCode;

	private Runnable getCodeBtnUpdate = new Runnable()
	{

		@Override
		public void run()
		{
			getCodeBtn.setText("获取时间：" + time + "秒");

			if (time > 0)
			{
				mHandler.postDelayed(this, 1000);
			}
			else
			{
				getCodeBtn.setEnabled(true);
				getCodeBtn.setText("获取验证码");
			}
			time--;

		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{

		View view = inflater.inflate(R.layout.fragement_reg_email, container,
				false);
		view.findViewById(R.id.reg_email_btn_back).setOnClickListener(this);
		view.findViewById(R.id.reg_email_next_btn).setOnClickListener(this);
		this.email = (TextView) view.findViewById(R.id.regEmail);
		getCodeBtn = (Button) view.findViewById(R.id.reg_getvf);
		getCodeBtn.setOnClickListener(this);
		regCode = (EditText) view.findViewById(R.id.regEmail_code);
		return view;
	}

	@Override
	public void onClick(View v)
	{

		switch (v.getId())
		{
		case R.id.reg_email_btn_back:
		{

			this.getActivity().finish();
			break;
		}
		case R.id.reg_email_next_btn:
		{

			if (this.email.getText().toString().length() == 0)
			{
				Toast.makeText(this.getActivity(), "邮箱不能为空", Toast.LENGTH_LONG)
						.show();
				return;
			}
			if (StringUtil.isEmailFormat(this.email.getText().toString()))
			{

				if (regCode.getText().toString().equals(this.code))
				{
					RegisterActivity ra = (RegisterActivity) this.getActivity();
					ra.setEmail(this.email.getText().toString());
					ra.goToNextPager(1);
				}
				else
				{
					Toast.makeText(this.getActivity(), "验证码不正确",
							Toast.LENGTH_LONG).show();
				}
			}
			else
			{
				Toast.makeText(this.getActivity(), "邮箱格式不正确", Toast.LENGTH_LONG)
						.show();
			}

			break;
		}
		case R.id.reg_getvf:
		{

			if (this.email.getText().toString().length() == 0)
			{
				Toast.makeText(this.getActivity(), "邮箱不能为空", Toast.LENGTH_LONG)
						.show();
				return;
			}
			if (StringUtil.isEmailFormat(this.email.getText().toString()))
			{
				Map<String, Object> paras = new HashMap<String, Object>();
				paras.put(MainServiceKey.EMAIL, this.email.getText().toString());
				MainServiceTask mst = new MainServiceTask(
						TaskType.REG_EMAIL_VF, paras,
						RegEmailFragement.class.getName());
				MainService.addActivityMap(RegEmailFragement.class.getName(),
						this);
				MainService.addTaskQueue(mst);
				time = 30;
				mHandler.postDelayed(getCodeBtnUpdate, 1000);
				getCodeBtn.setEnabled(false);
			}
			else
			{
				Toast.makeText(this.getActivity(), "邮箱格式不正确", Toast.LENGTH_LONG)
						.show();
			}

			break;
		}
		default:
			break;
		}

	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{
		switch (taskID)
		{
		case TaskType.REG_EMAIL_VF:
		{

			String resultStr = (String) paras[0];

			try
			{
				JSONObject jo = new JSONObject(resultStr);
				boolean isExist = jo.getBoolean("i");
				if (isExist)
				{
					Toast.makeText(this.getActivity(), "该邮箱已经注册过了,请选用别的邮箱！",
							Toast.LENGTH_LONG).show();
				}
				else
				{
					code = jo.getString("c");
				//	this.regCode.setText(code);
				}
			}
			catch (JSONException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		}
		default:
			break;
		}
		return null;
	}

	@Override
	public void onDestroy()
	{
		MainService.removeActivity(RegEmailFragement.class.getName());
		mHandler.removeCallbacks(getCodeBtnUpdate);
		super.onDestroy();
	}
}
